<?php

namespace App\Http\Controllers\Backstage\Column;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;

//栏目管理

class ColumnController extends Controller

{

    //栏目列表

    public function columnmanage(Request $request)
    {

        $token = $request->query('token');

        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        $info = DB::table('column')->get();

        if ($info) {

            $info = json_decode($info, true); //对象转成数组

            foreach ($info as $key => $value) {

                $info[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            }

            $data = success();

            $data['data'] = $info;
        } else {

            $data = fail();
        }

        return $data;
    }



    //添加栏目

    public function addcolumn(Request $request)
    {

        $token = $request->input('token');

        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        $data['column_title'] = $request->input('column_title');

        $data['create_time'] = time();

        $info = DB::table('column')->insert($data);

        if ($info) {

            $data = success();
        } else {

            $data = fail();
        }

        return $data;
    }



    //修改栏目

    public function editcolumn(Request $request)
    {

        $token = $request->input('token');

        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        $map['column_id'] = $request->input('column_id');

        $data['column_title'] = $request->input('column_title');

        $info = DB::table('column')->where($map)->update($data);

        if ($info) {

            $data = success();
        } else {

            $data = fail();
        }

        return $data;
    }



    //删除栏目

    public function delcolumn(Request $request)
    {

        $token = $request->query('token');

        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        $data['column_id'] = $request->input('column_id');

        $info = DB::table('column')->where($data)->delete();

        if ($info) {

            $data = success();
        } else {

            $data = fail();
        }

        return $data;
    }
}
